﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevUIUtils
{
    public static class FormStyleEx
    {
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
        [DllImport("user32.dll")]
        private static extern int ReleaseCapture();
        private const int WM_NCLBUTTONDOWN = 0XA1;   //定义鼠標左鍵按下
        private const int HTCAPTION = 2;

        /// <summary>
        /// 加载None窗体样式
        /// </summary>
        /// <param name="frm"></param>
        /// <param name="canMove">窗体是否可以移动</param>
        public static void LoadNoneStyle(this Form frm, bool canMove = true, Control moveContainer = null)
        {
            frm.AutoScaleMode = AutoScaleMode.Dpi;
            frm.FormBorderStyle = FormBorderStyle.None;
            if (canMove)
            {
                frm.MouseDown += (sender, e) =>
                {
                    ReleaseCapture();
                    SendMessage((int)frm.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
                };
                if (moveContainer != null)
                {
                    moveContainer.MouseDown += (sender, e) =>
                    {
                        ReleaseCapture();
                        SendMessage((int)frm.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
                    };
                }
            }

        }


    }
}
